<?php
session_start();
	function check_email_address($email) {
  // First, we check that there's one @ symbol, 
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
      return false;
    }
  }
  // Check if domain is IP. If not, 
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
↪([A-Za-z0-9]+))$",
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
	function getlang(){
		$lang = $_SESSION['lang'];
		if (empty($lang) || $lang == "KR") {
			$value = "value/value_kr.php";
		} else if($lang == "VN") {
			$value = "value/value_vn.php";
		}
		else if($lang == "EN"){
			$value = "value/value_en.php";
		}
		return $value;
	}
	
	$value = getlang();
	include($value);
	$lang = $_SESSION['lang'];
	if($lang==''){
		$lang="KR";
		$_SESSION['lang']=$lang;
	}
	
	if(isset($_REQUEST['Dangnhap'])){
		$tkemail = $_REQUEST['email'];
		
		$passwd= $_REQUEST['passwd'];
		$check= $_REQUEST['check'];
		
		if(empty($tkemail))
			$_SESSION['errordn']=$errorLogin1;
		else if(empty($passwd))
			$_SESSION['errordn']=$errorLogin2;
		else{
			include 'connection/connection.php';
			
			$passwd = md5($passwd);
				
			$query = mysql_query("select role from user_info where email='$tkemail' and passwd = '$passwd'");
			$co=0;
			while ($row=mysql_fetch_array($query)){
				$co=1;
			    if($_REQUEST['check'] == "on"){// Da tick
			    	setcookie("tkemail",$tkemail,time()+60*60*24*30);
			    	//setcookie("tkemailrestore",$row['emailrestore'],time()+200000);
			    	setcookie("role",$row['role'],time()+60*60*24*30);
			    }
			    else{ 
			    	setcookie('tkemail');
			    	//setcookie("tkemailrestore");
			    	setcookie("role");
			    } 
			    $_SESSION['tkemail']=$tkemail;
			    //$_SESSION['tkemailrestore']=$row['emailrestore'];
			    $_SESSION['role']=$row['role'];
			    $_SESSION['action']='hoso';
			  	//echo $_SESSION['role'];
			}
			if($co==0)
				$_SESSION['errordn']=$errorLogin3;
			//mysql_connect($connection);
		}
	}
	if(isset($_REQUEST['signup'])){
		//$emailrestore=$_REQUEST['emailrestore'];
		$nemail=$_REQUEST['email'];
		$passwd=$_REQUEST['passwd'];
		$copasswd=$_REQUEST['cpasswd'];
		$code=$_REQUEST['code'];
		$captcha=$_REQUEST['captcha'];
		// check email
		session_unregister('errordk');
		session_unregister('success');
		
		if(empty($nemail))
				$_SESSION['errordk']=$errorSignup1;
		
		else if(check_email_address($nemail)){
			//check password
			
			if(empty($passwd))
				$_SESSION['errordk']=$errorSignup2;
			else if(empty($copasswd) )
				$_SESSION['errordk']=$errorSignup3;
			/*else if(empty($emailrestore))
				$_SESSION['error']=$errorSignup9;
			else if(!check_email_address($emailrestore))
				$_SESSION['error']=$errorSignup10;
			else if($email==$emailrestore)
				$_SESSION['error']=$errorSignup11;*/
			else if($passwd==$copasswd){
				//check captcha
				if($captcha=="" )
					$_SESSION['errordk']=$errorSignup4;
				else if(strcasecmp($code,$captcha)==0){
					include 'connection/connection.php';
					$c=mysql_query("INSERT INTO user_info (email,passwd) VALUES ('$nemail', '".md5($passwd)."')");
					if ($c==1){
						//mysql_query("INSERT INTO license(email,id_type_license,status) VALUES ('$nemail', '1', '1')");
						mysql_query("INSERT INTO log_buy(email,buy_date,buy_type,use_month,date_start,date_stop) 
											VALUES ('$nemail',NOW(), '1', '12', NOW(), NOW() + INTERVAL 1 YEAR)");
						$_SESSION['action']='dangnhap';
					}
					else 
						$_SESSION['errordk']=$errorSignup5;
					//mysql_connect($connection);
				}else
					$_SESSION['errordk']=$errorSignup6;
			}else
				$_SESSION['errordk']=$errorSignup7;
		}	
		else
		    $_SESSION['errordk']=$errorSignup8;
	}
	
	if(isset($_REQUEST['changePass'])){
		$passold=$_REQUEST['passold'];
		//echo $passold;
		$passnew1=$_REQUEST['passnew1'];
		$passnew2=$_REQUEST['passnew2'];
		
		if(!isset($_SESSION['tkemail'])){
			if(!isset($_COOKIE['tkemail'])){
				$_SESSION['action']='login';
				header( 'Location: index.php' );
			}else {
				$_SESSION['tkemail']=$_COOKIE["tkemail"];
			    $_SESSION['tkemailrestore']=$_COOKIE["tkemailrestore"];
			    $_SESSION['role']=$_COOKIE["role"];
				
				$tkemail=$_SESSION['tkemail'];
			}
		}else 
			$tkemail=$_SESSION['tkemail'];
			
		if(empty($passold))
			$_SESSION['error']=$errorProfile1;
		else if(empty($passnew1))
			$_SESSION['error']=$errorProfile2;
		else if(empty($passnew2))
			$_SESSION['error']=$errorProfile3;
		else{
			include 'connection/connection.php';
			
			
			$passold = md5($passold);
				
			$query = mysql_query("select * from user_info where email='$tkemail' and passwd = '$passold'");
			//echo "select * from user_info where email='$tkemail' and passwd = '$passold'";
			$co=0;
			while ($row=mysql_fetch_array($query)){
				$co=1;
			}
			
			
			if($co==1)
			{
				if($passnew1==$passnew2){
					$passnew1 = md5($passnew1);
					$c = mysql_query("UPDATE user_info SET passwd='$passnew1' WHERE (email='$tkemail')");
					if($c==1)
						$_SESSION['success']=$errorProfile4;
				}else{
					$_SESSION['error']=$errorProfile5;
				}
			}
			else
				$_SESSION['error']=$errorProfile6;
			//mysql_connect($connection);
		}


	}
	
	$action = $_SESSION['action'];
	if($action==''){
		$action="home";
		$_SESSION['action']=$action;
	}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<title><?=$title?></title>
</head>
<body onload="initialize()">
<script type="text/javascript">
function changePage1(str)
{
	if (str==""){
		document.getElementById("txtHint").innerHTML="";
		return;
	} 
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("wrapper").innerHTML=xmlhttp.responseText;
										  }
									  }
	xmlhttp.open("GET","ajax/changePage1.php?action="+str,true);
	xmlhttp.send();
}

function changePage(str)
{
	window.open("ajax/changePage.php?action="+str,"_self");
	
}

</script>
<script type="text/javascript">
function changeCaptcha()
{
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("captcha").innerHTML=xmlhttp.responseText;
										  }
									  }
	xmlhttp.open("GET","changeCaptcha.php",true);
	xmlhttp.send();
}
function popuploadbel(ele){
	var popID = $(ele).attr('rel'); //Get Popup Name
    var popURL = $(ele).attr('href'); //Get Popup href to define size
                    
    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><span class="btn_close"></span></a>');
    
    //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;
    
    //Apply Margin to Popup
    $('#' + popID).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
    });
    
    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
    $('a.close, #fade, .iclose').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
              $('#fade, a.close').remove();  
	}); //fade them both out
	      
	      return false;
	});
}
</script>
<script type="text/javascript" 
src="http://maps.googleapis.com/maps/api/js?sensor=false&language=vi"></script>
<script type="text/javascript">
var map;
function initialize() {
      var myLatlng = new google.maps.LatLng( 37.487276,127.022042);
      var myOptions = {
    zoom: 14,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("div_id"), myOptions); 
  // Biến text chứa nội dung sẽ được hiển thị
var text;
text= "<b style='color:#00F' " + 
         "style='text-align:center'>(주)엘리소프트<br />" + 
     " 서울시 서초구 서초동 1617-34번지 3층 </b>";
   var infowindow = new google.maps.InfoWindow(
    { content: text,
        size: new google.maps.Size(100,50),
        position: myLatlng
    });
       infowindow.open(map);    
    var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map,
      title:"(주)엘리소프트!"
  });
}
</script>
<div id="wrapper">
	<div id="top">
    	<?php
			include 'page/top.php';
		?>
    </div>
    <div class="dmenu">
        <ul class="menu">
            <li class="home" id="selected"><a href="#" onclick="changePage('home')"><?=$home?></a></li>
            <li class="download2"><a href="#" onclick="changePage('download')"><?=$download?></a></li>
            <li class="buy"><a href="#" onclick="changePage('buy_Personal')"><?=$buy?></a></li>
            <li class="contact"><a href="#"  onclick="changePage('contact')"><?=$contact?></a></li>
        </ul>
    </div>
    <div class="clear"></div>
    <?php
			include $action.".php";
	?>
    <div class="footer">
    	<?php
			include 'page/footer.php';
		?>
    </div>
</div>
</body>
</html>
